﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for TimNhanVien.
	/// </summary>
	public class frmTimNhanVien : System.Windows.Forms.Form
	{
		private AccessData mDB=new AccessData();
        Language lan = new Language();
		private bool bClear=true;
		private string ma_nv,ho_nv,ten_nv,bo_phan;
		private string chon_manv,chon_manv1,chon_manv2;
		private DataSet ds=new DataSet();
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Panel panButLyLich;
		private System.Windows.Forms.Button butKetThuc;
		private System.Windows.Forms.Button butTim;
		private System.Windows.Forms.DataGrid dgrThanhVienGD;
		private System.Windows.Forms.Button butChon;
		private System.Windows.Forms.ComboBox cboBoPhan;
		private System.Windows.Forms.TextBox txtHoNV;
		private System.Windows.Forms.TextBox txtTenNV;
		private System.Windows.Forms.TextBox txtMaSoNV;
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        
		
		public frmTimNhanVien()
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);  
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmTimNhanVien));
            this.label12 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.panButLyLich = new System.Windows.Forms.Panel();
            this.butChon = new System.Windows.Forms.Button();
            this.butKetThuc = new System.Windows.Forms.Button();
            this.butTim = new System.Windows.Forms.Button();
            this.dgrThanhVienGD = new System.Windows.Forms.DataGrid();
            this.cboBoPhan = new System.Windows.Forms.ComboBox();
            this.txtHoNV = new System.Windows.Forms.TextBox();
            this.txtTenNV = new System.Windows.Forms.TextBox();
            this.txtMaSoNV = new System.Windows.Forms.TextBox();
            this.panButLyLich.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dgrThanhVienGD)).BeginInit();
            this.SuspendLayout();
            // 
            // label12
            // 
            this.label12.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label12.ForeColor = System.Drawing.Color.DarkBlue;
            this.label12.Location = new System.Drawing.Point(56, 6);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(48, 24);
            this.label12.TabIndex = 0;
            this.label12.Text = "Mã số ";
            this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.ForeColor = System.Drawing.Color.DarkBlue;
            this.label3.Location = new System.Drawing.Point(177, 11);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(46, 18);
            this.label3.TabIndex = 2;
            this.label3.Text = "Họ tên:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(48, 37);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(56, 18);
            this.label2.TabIndex = 7;
            this.label2.Text = "Bộ phận ";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // panButLyLich
            // 
            this.panButLyLich.Controls.Add(this.butChon);
            this.panButLyLich.Controls.Add(this.butKetThuc);
            this.panButLyLich.Controls.Add(this.butTim);
            this.panButLyLich.Location = new System.Drawing.Point(135, 320);
            this.panButLyLich.Name = "panButLyLich";
            this.panButLyLich.Size = new System.Drawing.Size(225, 42);
            this.panButLyLich.TabIndex = 8;
            // 
            // butChon
            // 
            this.butChon.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butChon.ForeColor = System.Drawing.Color.DarkBlue;
            this.butChon.Image = ((System.Drawing.Image)(resources.GetObject("butChon.Image")));
            this.butChon.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butChon.Location = new System.Drawing.Point(75, 5);
            this.butChon.Name = "butChon";
            this.butChon.Size = new System.Drawing.Size(60, 28);
            this.butChon.TabIndex = 1;
            this.butChon.Text = "Chọn";
            this.butChon.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butChon.Click += new System.EventHandler(this.butChon_Click);
            // 
            // butKetThuc
            // 
            this.butKetThuc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKetThuc.ForeColor = System.Drawing.Color.DarkBlue;
            this.butKetThuc.Image = global::Human.Properties.Resources.close_r;
            this.butKetThuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetThuc.Location = new System.Drawing.Point(141, 5);
            this.butKetThuc.Name = "butKetThuc";
            this.butKetThuc.Size = new System.Drawing.Size(75, 28);
            this.butKetThuc.TabIndex = 2;
            this.butKetThuc.Text = "Kết thúc";
            this.butKetThuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetThuc.Click += new System.EventHandler(this.butKetThuc_Click);
            // 
            // butTim
            // 
            this.butTim.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butTim.ForeColor = System.Drawing.Color.DarkBlue;
            this.butTim.Image = global::Human.Properties.Resources.t_search;
            this.butTim.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butTim.Location = new System.Drawing.Point(9, 5);
            this.butTim.Name = "butTim";
            this.butTim.Size = new System.Drawing.Size(60, 28);
            this.butTim.TabIndex = 0;
            this.butTim.Text = "Tìm";
            this.butTim.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butTim.Click += new System.EventHandler(this.butTim_Click);
            this.butTim.KeyDown += new System.Windows.Forms.KeyEventHandler(this.butTim_KeyDown);
            // 
            // dgrThanhVienGD
            // 
            this.dgrThanhVienGD.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dgrThanhVienGD.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dgrThanhVienGD.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgrThanhVienGD.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dgrThanhVienGD.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dgrThanhVienGD.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrThanhVienGD.CaptionText = "Thông tin nhân viên";
            this.dgrThanhVienGD.CaptionVisible = false;
            this.dgrThanhVienGD.DataMember = "";
            this.dgrThanhVienGD.FlatMode = true;
            this.dgrThanhVienGD.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dgrThanhVienGD.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrThanhVienGD.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dgrThanhVienGD.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dgrThanhVienGD.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dgrThanhVienGD.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dgrThanhVienGD.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrThanhVienGD.LinkColor = System.Drawing.Color.Teal;
            this.dgrThanhVienGD.Location = new System.Drawing.Point(9, 66);
            this.dgrThanhVienGD.Name = "dgrThanhVienGD";
            this.dgrThanhVienGD.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dgrThanhVienGD.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrThanhVienGD.ReadOnly = true;
            this.dgrThanhVienGD.RowHeaderWidth = 10;
            this.dgrThanhVienGD.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dgrThanhVienGD.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrThanhVienGD.Size = new System.Drawing.Size(502, 242);
            this.dgrThanhVienGD.TabIndex = 6;
            this.dgrThanhVienGD.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dgrThanhVienGD_KeyDown);
            // 
            // cboBoPhan
            // 
            this.cboBoPhan.DropDownWidth = 128;
            this.cboBoPhan.ItemHeight = 13;
            this.cboBoPhan.Location = new System.Drawing.Point(110, 34);
            this.cboBoPhan.Name = "cboBoPhan";
            this.cboBoPhan.Size = new System.Drawing.Size(340, 21);
            this.cboBoPhan.TabIndex = 5;
            this.cboBoPhan.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboBoPhan_KeyDown);
            // 
            // txtHoNV
            // 
            this.txtHoNV.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtHoNV.Location = new System.Drawing.Point(227, 9);
            this.txtHoNV.Name = "txtHoNV";
            this.txtHoNV.Size = new System.Drawing.Size(150, 21);
            this.txtHoNV.TabIndex = 3;
            this.txtHoNV.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtHoNV_KeyDown);
            // 
            // txtTenNV
            // 
            this.txtTenNV.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtTenNV.Location = new System.Drawing.Point(378, 9);
            this.txtTenNV.Name = "txtTenNV";
            this.txtTenNV.Size = new System.Drawing.Size(70, 21);
            this.txtTenNV.TabIndex = 4;
            this.txtTenNV.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtTenNV_KeyDown);
            // 
            // txtMaSoNV
            // 
            this.txtMaSoNV.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtMaSoNV.Location = new System.Drawing.Point(110, 9);
            this.txtMaSoNV.Name = "txtMaSoNV";
            this.txtMaSoNV.Size = new System.Drawing.Size(65, 21);
            this.txtMaSoNV.TabIndex = 1;
            this.txtMaSoNV.Validated += new System.EventHandler(this.txtMaSoNV_Validated);
            this.txtMaSoNV.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtMaSoNV_KeyDown);
            // 
            // frmTimNhanVien
            // 
            this.AcceptButton = this.butTim;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(524, 375);
            this.Controls.Add(this.txtMaSoNV);
            this.Controls.Add(this.txtTenNV);
            this.Controls.Add(this.txtHoNV);
            this.Controls.Add(this.cboBoPhan);
            this.Controls.Add(this.dgrThanhVienGD);
            this.Controls.Add(this.panButLyLich);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label12);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmTimNhanVien";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Tìm nhân viên";
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmTimNhanVien_MouseMove);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmTimNhanVien_KeyDown);
            this.Load += new System.EventHandler(this.frmTimNhanVien_Load);
            this.panButLyLich.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dgrThanhVienGD)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void butKetThuc_Click(object sender, System.EventArgs e)
		{
			chon_manv="";chon_manv1="";chon_manv2="";
			this.Close();
		}
		private void frmTimNhanVien_Load(object sender, System.EventArgs e)
		{
			load_bophan();
			cboBoPhan.SelectedIndex=-1;
		}
		private void ktcbo()
		{
			cboBoPhan.SelectedIndex=-1;
		}
		private void load_bophan()
		{
			cboBoPhan.DisplayMember="tenbp";
			cboBoPhan.ValueMember="madv";
			cboBoPhan.DataSource=mDB.get_bophan().Tables[0];
		}

		private void butTim_Click(object sender, System.EventArgs e)
		{
			try
			{				
				dgrThanhVienGD.DataSource=null;
			    LoadTim_NV(); 
			}
			catch{}
		}
		private void butChon_Click(object sender, System.EventArgs e)
		{
		//	frmLyLichNhanVien frm=new frmLyLichNhanVien();
			try
			{	
				chon_manv= dgrThanhVienGD[dgrThanhVienGD.CurrentRowIndex,0].ToString();								
				chon_manv1= dgrThanhVienGD[dgrThanhVienGD.CurrentRowIndex,1].ToString();								
				chon_manv2= dgrThanhVienGD[dgrThanhVienGD.CurrentRowIndex,2].ToString();								
				this.Close();				
			}
			catch
			{
				MessageBox.Show(lan.Change_language_MessageText("Đề nghị chọn nhân viên"),AccessData.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Error);
				txtMaSoNV.Focus();
			}
		}
		public string get_manv
		{
			get
			{
				return chon_manv;
			}
		}
		public string get_manv1
		{
			get
			{
				return chon_manv1;
			}
		}
		public string get_manv2
		{
			get
			{
				return chon_manv2;
			}
		}
	    private void txtMaSoNV_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{

				//SendKeys.Send("{Tab}");
				txtHoNV.Focus();

			}
		}
		private void txtHoNV_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				SendKeys.Send("{Tab}");	
				
			}	
		}
		private void txtTenNV_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				SendKeys.Send("{Tab}");	
				//cboBoPhan.Focus();
			}
		}
		private void cboBoPhan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				SendKeys.Send("{Tab}");			
				//butTim.Focus();
			}			
		}
		private void txtMaSoNV_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (txtMaSoNV.TextLength==0)
				SendKeys.Send("{Tab}");	
			else 
				txtMaSoNV.Text=Caps(txtMaSoNV.Text.ToString());
		}

		private void txtTenNV_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (txtTenNV.TextLength==0)
				SendKeys.Send("{Tab}");
			else 
				txtTenNV.Text=Caps(txtTenNV.Text.ToString());
		}
		public string Caps( string s)
		{
			StringBuilder sb = new StringBuilder(s);
			sb[0] = Char.ToUpper( sb[0]);
			string ret=null;			
			int num = 0;int ispace =0;
			while(num < sb.Length)
			{
				if(Char.IsWhiteSpace(sb[num]))
				{
					//num++;
					ispace++;
				}
			
				if(!Char.IsWhiteSpace(sb[num])) 							
				{
					if (ispace>0 && num>0)
					{
						sb[num] = Char.ToUpper( sb[num]);
						ispace=0;
					}
				}				
				num++;				
			}
			num = 0;
			ispace=0;
			while(num < sb.Length)
			{
				if(Char.IsWhiteSpace(sb[num]))
				{
					if (ispace<1 && num>0 )
					{
						ret+=sb[num];						
					}
					ispace++;
				}
				else
				{
					ret+=sb[num];
					ispace=0;
				}				
				num++;
			}
			return ret;
		}

		private void LoadTim_NV()
		{
            ma_nv = txtMaSoNV.Text.Trim().PadLeft(5, '0') ;
            ho_nv = txtHoNV.Text.Trim().ToUpper();
			ten_nv=txtTenNV.Text.Trim().ToUpper();
			bo_phan=(cboBoPhan.SelectedIndex==-1)?"":cboBoPhan.SelectedValue.ToString();
			ds=mDB.TimNV(ma_nv,ho_nv,ten_nv,bo_phan);
			dgrThanhVienGD.TableStyles.Clear();
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = ds.Tables[0].TableName;
					
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
				
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "mnvmay";
			TextCol1.HeaderText = lan.Change_language_MessageText("Mã NV");
			TextCol1.Width = 50;
			TextCol1.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol1);
			dgrThanhVienGD.TableStyles.Add(ts);				
			
			DataGridTextBoxColumn TextCol2=new DataGridTextBoxColumn();
			TextCol2.MappingName = "ho";
			TextCol2.HeaderText = lan.Change_language_MessageText("Họ Tên NV");
			TextCol2.Width = 200;
			TextCol2.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol2);
			dgrThanhVienGD.TableStyles.Add(ts);			
			
			DataGridTextBoxColumn TextCol4=new DataGridTextBoxColumn();
			TextCol4.MappingName = "tenbp";
			TextCol4.HeaderText = lan.Change_language_MessageText("Bộ Phận");
			TextCol4.Width = 150;
			TextCol4.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol4);
			dgrThanhVienGD.TableStyles.Add(ts);
			
			DataGridTextBoxColumn TextCol5=new DataGridTextBoxColumn();
			TextCol5.MappingName = "diengiai";
			TextCol5.HeaderText = lan.Change_language_MessageText("Chức Vụ");
			TextCol5.Width = 100;
			TextCol5.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol5);
			dgrThanhVienGD.TableStyles.Add(ts);
			
			ts.DataGrid.DataSource=ds.Tables[0]; 
		}

		private void frmTimNhanVien_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(bClear)
			{
				bClear=false;
				ktcbo();
			}
		}

		private bool IsNumeric (string strNum)
		{	
			int iNum;
			try
			{
				iNum = int.Parse(strNum);
				return true;
			}
			catch
			{
				return false;
			}
		}

		private void txtHoNV_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
//			if (IsNumeric(e.KeyChar.ToString()))
//				e.Handled = true;
//			if(e.KeyChar==8)
//				e.Handled=false;
		}

		private void butTim_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode.ToString()=="Enter")
				dgrThanhVienGD.Focus();
		}

		private void dgrThanhVienGD_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode.ToString()=="Enter")
				butChon.Focus();
		}

		public string kq
		{
			get
			{
				chon_manv = dgrThanhVienGD[dgrThanhVienGD.CurrentRowIndex,0].ToString();
				return chon_manv;
			}
		}
		public string kq1
		{
			get
			{
				chon_manv1 = dgrThanhVienGD[dgrThanhVienGD.CurrentRowIndex,1].ToString();
				return chon_manv1;
			}
		}
		public string kq2
		{
			get
			{
				chon_manv2 = dgrThanhVienGD[dgrThanhVienGD.CurrentRowIndex,2].ToString();
				return chon_manv2;
			}
		}

        private void txtMaSoNV_Validated(object sender, EventArgs e)
        {
            ma_nv = txtMaSoNV.Text.Trim().PadLeft(5, '0');
        }

        private void frmTimNhanVien_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.KeyCode == Keys.T)
            {
                butTim_Click(null, null);
            }
            if (e.Alt && e.KeyCode == Keys.C)
            {
                butChon_Click(null, null);
            }
            if (e.Alt && e.KeyCode == Keys.K)
            {
                butKetThuc_Click(null, null);
            }
        }
	}
}
